package com.donica.wamstest.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.donica.wamstest.entity.PageResult;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.entity.TaskFindVO;
import com.donica.wamstest.entity.TaskVO;
import com.donica.wamstest.mapper.TaskMapper;

@Service

public class TaskService {
	@Autowired
	TaskMapper taskMapper;

	boolean statusUpdated = false;

	public long InsertTask(List<TaskVO> volist) {
		for (TaskVO taskVO : volist)
			taskVO.initNullItem();
		return this.taskMapper.InsertTask(volist);
	}

	public long UpdateTask(List<TaskVO> volist) {
		if (volist.size() > 0) {
			this.statusUpdated = true;
			return this.taskMapper.UpdateTask(volist);
		}
		return 0L;
	}

	public long deleteTask(List<TaskVO> volist) {
		return this.taskMapper.deleteTask(volist);
	}

	public ReturnMessage searchTask(TaskFindVO vo) {
		ReturnMessage rtnMsg = new ReturnMessage();
		try {
			long total = this.taskMapper.searchTaskCount(vo);
			List<TaskVO> result = this.taskMapper.searchTask(vo);
			PageResult pageResult = new PageResult();
			pageResult.setTotalCount(total);
			pageResult.setPageSize(vo.getPageSize());
			pageResult.setCurrPage(vo.getCurrPage());
			pageResult.setResult(result);
			rtnMsg.setResult(pageResult);
		} catch (Exception e) {
			rtnMsg.setCode(0);
			rtnMsg.setMsg(e.getMessage());
		}
		return rtnMsg;
	}

	public long calTaskProcess() {
		return this.taskMapper.calTaskProcess();
	}
	@Scheduled(initialDelay = 1000L, fixedDelay = 5000L)
	  public void calTaskProcessTimer() {
	    System.out.println("calTaskProcessTimer 当前时间:" + new Date());
	    //if (this.statusUpdated) 
	    {
	      calTaskProcess();
	      this.statusUpdated = false;
	    } 
	 }
}